package com.yunji.system.provide;

import com.yunji.common.cache.RedisCache;
import com.yunji.common.constant.Constants;
import com.yunji.common.model.domain.SysUserDO;
import com.yunji.common.utils.StringUtils;
import com.yunji.system.service.ISysConfigService;
import com.yunji.system.service.ISysUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.system.provide
 * @Title: SystemProvide
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-11-02 16:25:52      V1.0        HuaAo       新建类
 */
@Component
public class SystemProvide {


    @Resource
    ISysUserService sysUserService;

    @Resource
    ISysConfigService sysConfigService;

    @Resource
    RedisCache redisCache;

    public void deleteUserToken(Long belongId) {
        // 找到当前belongId的用户 退出登录
        SysUserDO userParam = new SysUserDO();
        userParam.setBelongId(belongId);
        List<SysUserDO> userList = sysUserService.searchUserList(userParam);
        for (SysUserDO user : userList) {
            String redisToken = StringUtils.format(Constants.REDIS_BELONG_UUID, belongId) + user.getUserId();
            if (redisCache.hasKey(redisToken)) {
                String uuid = redisCache.get(redisToken);
                redisCache.delete(uuid);
                redisCache.delete(redisToken);
            }
        }

    }
    public String selectConfigByKey(String configKey) {
        return sysConfigService.selectConfigByKey(configKey);
    }
}